package com.yrot.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yrot.admin.entity.SysExpert;
import com.yrot.admin.entity.SysQuestionBank;
import com.yrot.admin.form.ExpertValid;
import com.yrot.admin.service.ISysExpertService;
import com.yrot.common.annotation.SysLog;
import com.yrot.common.result.R;
import com.yrot.common.result.Result;
import com.yrot.common.result.ResultUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hemin
 * @since 2019-04-02
 */
@RestController
@RequestMapping("/admin/expert")
public class SysExpertController {

    @Autowired
    ISysExpertService expertService;

    @GetMapping("/experts")
    public Result experts(int page, int limit,
                          @RequestParam(required = false) String expertName,
                          @RequestParam(required = false) String username){
        QueryWrapper<SysExpert> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("expert_id");
        queryWrapper.eq("role_name","EXPERT");
        if(StringUtils.isNotEmpty(expertName)){
            queryWrapper.like("expert_name",expertName);
        }
        if(StringUtils.isNotEmpty(username)){
            queryWrapper.like("expert_username",username);
        }
        Page<SysExpert> pages = new Page<>(page, limit);
        IPage<SysExpert> expertPage = expertService.page(pages, queryWrapper);
        return ResultUtils.success(expertPage.getTotal(),expertPage.getRecords());
    }

    @PostMapping("/expert")
    public R saveExpert (@Valid ExpertValid expertValid, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
        SysExpert byUserName = expertService.findByUserName(expertValid.getExpertUsername());
        if(byUserName != null){
            return R.error("登录账号已存在");
        }
        SysExpert sysExpert = new SysExpert();
        BeanUtils.copyProperties(expertValid,sysExpert);
        sysExpert.setExpertPassword("123456");
        expertService.save(sysExpert);
        return R.ok();
    }

    @PutMapping("/expert")
    public R putExpert (@Valid ExpertValid expertValid, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
        SysExpert sysExpert = new SysExpert();
        BeanUtils.copyProperties(expertValid,sysExpert);
        expertService.saveOrUpdate(sysExpert);
        return R.ok();
    }

    /**
     * 重置密码
     */
    @PutMapping("/password/{expertId}")
    public R putPassword (@PathVariable("expertId") Integer expertId){
        SysExpert sysExpert = new SysExpert();
        sysExpert.setExpertId(expertId).setExpertPassword("123456");
        boolean status = expertService.updateById(sysExpert);
        if(status){
            return R.ok("重置密码成功，默认密码123456");
        }else{
            return R.error("重置密码失败");
        }
    }

    /**
     * 修改密码
     */
    @PutMapping("/newPassword")
    @SysLog("修改密码")
    public R putPassword (@RequestParam("oldPassword")String oldPassword,
                          @RequestParam("newPassword")String newPassword){
        SysExpert sysExpert = (SysExpert) SecurityUtils.getSubject().getPrincipal();
        if(!sysExpert.getExpertPassword().equals(oldPassword)){
            return R.error("密码错误");
        }
        sysExpert.setExpertPassword(newPassword);
        expertService.saveOrUpdate(sysExpert);
        return R.ok();
    }

    /**
     * 删除专家信息
     */
    @DeleteMapping("/expert/{expertId}")
    public void deleteExpert(@PathVariable("expertId") Integer expertId){
        expertService.removeById(expertId);
    }

    /**
     * 查询当前登陆的人的信息
     */
    @GetMapping("/expert")
    public SysExpert expert(){
        SysExpert sysExpert = (SysExpert) SecurityUtils.getSubject().getPrincipal();
        return expertService.getById(sysExpert.getExpertId());
    }

}
